package example9_GUI;

import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.StringTokenizer;

import javax.swing.*;

public class HandleListener implements ActionListener {
	JTextArea inputText,showText;
	JTextField replace,beReplaced;

	public void setInputText(JTextArea text) {
		inputText = text;
	}

	public void setShowText(JTextArea text) {
		showText = text;
	}
	public void setReplaceField(JTextField f) {
		replace=f;
	}
	public void setReplacedField(JTextField f) {
		beReplaced=f;
	}

	public void actionPerformed(ActionEvent e) {
		String str = e.getActionCommand();
		if (str.equals("copy"))
			showText.copy();
		else if (str.equals("cut"))
			showText.cut();
		else if (str.equals("paste")) {
			
			inputText.paste();
			JOptionPane dlg = new JOptionPane();
			dlg.showMessageDialog(null, "成功");
		}
			
		else if(str.equals("save")) {
			File fWrite = new File("D:\\example.txt");
			if(!fWrite.exists()) {
				try {
					fWrite.createNewFile();
				}
				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			try {
				Writer out = new FileWriter(fWrite);
				BufferedWriter bufferWrite = new BufferedWriter(out);
				// Reader in = new FileReader(fRead);
				// BufferedReader bufferRead =new BufferedReader(in);
				//String str1 = null;
				
				//while (showText.getText()) {
					//StringTokenizer fenxi = new StringTokenizer(str1);
					//int count = fenxi.countTokens();
					//str1 = str1 + " 句子中单词个数:" + count;
					//bufferWrite.write(str1);
					//bufferWrite.newLine();
				//}
				for(char c:showText.getText().toString().toCharArray()) {
					
					if(c!=',') {
						bufferWrite.write(c);
						
					}
						
					else {
						bufferWrite.newLine();
					}
				}
				bufferWrite.close();
				out.close();
				//in = new FileReader(fWrite);
				//bufferRead = new BufferedReader(in);
				//String s = null;
				//System.out.println(fWrite.getName() + "内容:");
				// while((s=bufferRead.readLine())!=null) {
				// System.out.println(s);
				// }
				//bufferRead.close();
				//in.close();
			}
			catch (IOException e4) {
				System.out.println(e4.toString());
			}
			JOptionPane dlg = new JOptionPane();
			dlg.showMessageDialog(null, "成功");
		}
		else if(str.equals("open")) {
			
			//File fRead = new File("C:\\Users\\hp\\Desktop\\record.txt");
			File fRead = new File("D:\\abc.txt");
			//File fRead = new File("D:\\example.txt");
			
			//File fWrite = new File("C:\\Users\\hp\\Desktop\\record.txt");
			try {
				Reader in = new FileReader(fRead);
				BufferedReader bufferRead =new BufferedReader(in);
				String str1 = null;
				//in = new FileReader(fWrite);
				bufferRead = new BufferedReader(in);
				String s = null;
				//System.out.println(fWrite.getName() + "内容:");
				StringBuffer sb=new StringBuffer();
				while((s=bufferRead.readLine())!=null) {
					showText.append(s);
					showText.append(",");
					
				}
				bufferRead.close();
				in.close();
			}
			catch(IOException e5) {
				e5.printStackTrace();
			}
			
		}
		else if(str.equals("replace")) {
			//File fRead = new File("C:\\Users\\hp\\Desktop\\record.txt");
			//File fRead = new File("D:\\abc.txt");
			
			String tmp=showText.getText();
			System.out.println(tmp);
			String rp=replace.getText();//
			String brp=beReplaced.getText();
			System.out.println(rp);
			System.out.println(brp);
			tmp=tmp.replace(brp, rp);
			
			showText.setText(tmp);
			//System.out.println(tmp);
			
			File fWrite = new File("D:\\abc.txt");
			if(!fWrite.exists()) {
				try {
					fWrite.createNewFile();
				}
				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			try {
				Writer out = new FileWriter(fWrite);
				BufferedWriter bufferWrite = new BufferedWriter(out);
				
				for(char c:tmp.toString().toCharArray()) {
					
					if(c!=',') {
						bufferWrite.write(c);
						
					}
						
					else {
						bufferWrite.newLine();
					}
				}
				bufferWrite.close();
				out.close();
				
			}
			catch (IOException e4) {
				System.out.println(e4.toString());
			}
			JOptionPane dlg = new JOptionPane();
			dlg.showMessageDialog(null, "成功");
		
		
		}
		else if(str.equals("calc")) {
			String tmp=showText.getText();
			String regexChn="[\\u4e00-\\u9fa5]";
			String regexEng="[A-Za-z]";
			int numC=0;
			int numE=0;
			for(Character c:tmp.toCharArray()) {
				if(c.toString().matches(regexChn)) {
					numC++;
				}
				else if(c.toString().matches(regexEng)) {
					numE++;
				}
			}
			replace.setText("chn:"+ numC);
			beReplaced.setText("eng:"+numE);
		}
	}
}
